﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Web.UI.WebControls;

namespace QuanLyVanBanBUS
{
    public class TrangThaiBUS
    {
        public void themTrangThai(TrangThai ttDTO)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            storeDB.TrangThais.InsertOnSubmit(ttDTO);

            storeDB.SubmitChanges();
        }

        public void suaTrangThai(TrangThai ttDTO, string maTrangThai)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = storeDB.TrangThais.Where(q => q.MaTrangThai == maTrangThai).First();
            query.TenTrangThai = ttDTO.TenTrangThai;

            storeDB.SubmitChanges();
        }

        public void xoaTrangThai(GridView gridView)
        {
            //Lay MaTrangThai duoc chon tu CheckBox
            string temp = string.Empty;

            for (int i = 0; i < gridView.Rows.Count; i++)
            {
                CheckBox _checkBox = (CheckBox)(gridView.Rows[i].Cells[0].FindControl("chkMaTrangThai"));

                if (_checkBox.Checked == true)
                {
                    string maTrangThaiCanXoa = gridView.DataKeys[i].Value.ToString();

                    temp += maTrangThaiCanXoa + ",";
                }
            }

            //Kiem tra co check vao CheckBox khong?
            if (temp.Length - 1 > 0)
            {
                temp = temp.Substring(0, temp.Length - 1);
            }
            else
            {
                //Response.Write("<script>alert('Xin vui lòng chọn Sản phẩm cần xóa')</script>");

                return;
            }

            //Xoa
            QLVBDataContext storeDB = new QLVBDataContext();

            //Them cac MaTrangThai duoc chon vao mang
            string[] m_temp = new string[] { temp };

            while (temp.Length > 0)
            {
                //Lay maTemp de lam dieu kien so sanh. Lay tu sau lay toi cua Mang
                string maTemp = temp.Substring(temp.LastIndexOf(",") + 1, temp.Length - (temp.LastIndexOf(",") + 1));

                var query = storeDB.TrangThais.Where(q => q.MaTrangThai == maTemp).First();

                storeDB.TrangThais.DeleteOnSubmit(query);

                storeDB.SubmitChanges();

                //Kiem tra dieu kien co con trong vong lap While khong?
                if ((temp.Length - (maTemp.Length + 1)) > 0)
                {
                    temp = temp.Substring(0, temp.Length - (maTemp.Length + 1));
                }
                else
                    break;
            }

            gridView.DataSource = layDanhSachTrangThai();
            gridView.DataBind();
        }

        public IList<TrangThai> layDanhSachTrangThai()
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var lstTrangThai = storeDB.TrangThais.ToList();

            return lstTrangThai;
        }

        public Boolean kiemtraTonTaiMaTrangThai(string maTrangThai)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = storeDB.TrangThais.Where(q => q.MaTrangThai == maTrangThai).ToList();

            if (query.Count > 0)
            {
                return true;
            }

            return false;
        }

        public Boolean kiemtraTonTaiTenTrangThai(string tenTrangThai)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = storeDB.TrangThais.Where(q => q.TenTrangThai == tenTrangThai).ToList();

            if (query.Count > 0)
            {
                return true;
            }

            return false;
        }

        public string layMaTrangThai(string maTrangThai)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var _maTrangThai = storeDB.TrangThais.Where(m => m.MaTrangThai == maTrangThai).Select(m => m.MaTrangThai).First();

            return _maTrangThai;
        }

        public string layTenTrangThai(string maTrangThai)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var tenTrangThai = storeDB.TrangThais.Where(m => m.MaTrangThai == maTrangThai).Select(m => m.TenTrangThai).First();

            return tenTrangThai;
        }

        public void themItemVaoDropDownList(DropDownList dropDownList)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = storeDB.TrangThais.ToList();
            foreach (var q in query)
            {
                var maTrangThai = q.MaTrangThai;
                var tenTrangThai = q.TenTrangThai;

                ListItem item = new ListItem();
                item.Text = tenTrangThai;
                item.Value = maTrangThai;

                dropDownList.Items.Add(item);
            }
        }
    }
}
